package com.bidepan.spring6.xmltx.service;

import com.bidepan.spring6.xmltx.dao.BookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional(propagation= Propagation.REQUIRES_NEW)
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;

    //买书：传入图书id和用户id
    @Override
    public void buyBook(Integer bookId, Integer userId) {
        //根据图书id先查询图书：价格
        Integer price = bookDao.getPriceByBookId(bookId);
        //更新图书：库存-1
        bookDao.updateStock(bookId);
        //更新用户表：余额-图书价格
        bookDao.updateUserBalance(userId,price);
    }

}
